package XmlNetty;

import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.Bootstrap;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.serialization.ClassResolver;
import org.jboss.netty.handler.codec.serialization.ClassResolvers;
import org.jboss.netty.handler.codec.serialization.ObjectDecoder;
import org.jboss.netty.handler.codec.serialization.ObjectEncoder;
import org.jboss.netty.handler.timeout.IdleStateHandler;

public class byteToXmlServer {
	/**
	 * 
	 */
	public static volatile Map<String, Channel> channelMap = new HashMap<String, Channel>();
	public static void run() {
		// TODO Auto-generated method stub
		ChannelFactory factory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
		ServerBootstrap bootstrap = new ServerBootstrap(factory);
		
		bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
		@Override
			public ChannelPipeline getPipeline() throws Exception {
				// TODO Auto-generated method stub
				ChannelPipeline pipeline = Channels.pipeline();
				pipeline.addLast("encoding", new ObjectEncoder());
				pipeline.addLast("decoding", new ObjectDecoder(ClassResolvers.cacheDisabled(XmlPackge.class.getClassLoader())));
				pipeline.addLast("handle", new byteToXmlServerHandle());			
				return pipeline;
			}
		});
		bootstrap.setOption("child.tcpNoDelay", true);    
        bootstrap.setOption("child.keepAlive", true);    
            
        bootstrap.bind(new InetSocketAddress(8080));
	}
	
	public static void main(String[] args) throws Exception {
		run();
		for(;;){
			Thread.sleep(20000);
			System.out.println(channelMap);
		}
	}
}
